package com.lingchen.boot.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lingchen.boot.mapper.UserMapping;
import com.lingchen.boot.pojo.User;
import com.lingchen.boot.vo.Tongji;
import com.lingchen.boot.vo.Tongji2;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UserService {
    @Autowired
    UserMapping userMapping;

    public List<User> findAll(){
        return userMapping.selectAll();
    }

    public void batchInsert(){
        String name = "name";
        for (int k = 0; k < 300; k++) {
            List<User> list = new ArrayList<User>();
            for (int i = 0; i < 10000; i++) {
                int age = RandomUtils.nextInt(0, 100000);
                User user = new User();
                user.setName(name);
                user.setAge(age);
                list.add(user);
            }
            System.out.println("insert:"+k+"w");
            userMapping.insertList(list);
        }

    }
    public static void main(String[] args) {
        List<Tongji2> res= new ArrayList<>();
        List<Tongji> t3 = ttt(SaveJson.s3, "2021-11-25");
        List<Tongji> t4 = ttt(SaveJson.s4, "2021-11-26");
        List<Tongji> t5 = ttt(SaveJson.s5, "2021-11-27");
        List<Tongji> t6 = ttt(SaveJson.s6, "2021-11-28");
        List<Tongji> t7 = ttt(SaveJson.s7, "2021-11-29");
        List<Tongji> t8 = ttt(SaveJson.s8, "2021-11-30");
        List<Tongji> t9 = ttt(SaveJson.s9, "2021-12-01");

        for (Tongji tongji : t3) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS3(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS3(tongji.getCount());
            }
        }
        for (Tongji tongji : t4) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS4(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS4(tongji.getCount());
            }
        }
        for (Tongji tongji : t5) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS5(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS5(tongji.getCount());
            }
        }
        for (Tongji tongji : t6) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS6(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS6(tongji.getCount());
            }
        }
        for (Tongji tongji : t7) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS7(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS7(tongji.getCount());
            }
        }

        for (Tongji tongji : t8) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS8(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS8(tongji.getCount());
            }
        }
        for (Tongji tongji : t9) {
            Tongji2 tongji2 = res.stream().filter(p -> p.getAppName().equals(tongji.getAppName()) && p.getApi().equals(tongji.getApi())).findFirst().orElse(null);
            if (null == tongji2){
                tongji2 = new Tongji2();
                tongji2.setAppName(tongji.getAppName());
                tongji2.setApi(tongji.getApi());
                tongji2.setS9(tongji.getCount());
                res.add(tongji2);
            }else {
                tongji2.setS9(tongji.getCount());
            }
        }

        System.out.println("appName\tapi\tall_count\t2011-11-25\t2011-11-26\t2011-11-27\t2011-11-28\t2011-11-29\t2011-11-30\t2011-12-01");


        for (Tongji2 re : res) {
            int i3 = StringUtils.isEmpty(re.getS3()) ? 0 : Integer.valueOf(re.getS3());
            int i4 = StringUtils.isEmpty(re.getS4()) ? 0 : Integer.valueOf(re.getS4());
            int i5 = StringUtils.isEmpty(re.getS5()) ? 0 : Integer.valueOf(re.getS5());
            int i6 = StringUtils.isEmpty(re.getS6()) ? 0 : Integer.valueOf(re.getS6());
            int i7 = StringUtils.isEmpty(re.getS7()) ? 0 : Integer.valueOf(re.getS7());
            int i8 = StringUtils.isEmpty(re.getS8()) ? 0 : Integer.valueOf(re.getS8());
            int i9 = StringUtils.isEmpty(re.getS9()) ? 0 : Integer.valueOf(re.getS9());
            int all=i3+i4+i5+i6+i7+i8+i9;
            System.out.println(re.getAppName()+"\t"+re.getApi()+"\t"+all+"\t"+re.getS3()+"\t"+re.getS4()+"\t"+re.getS5()+"\t"+re.getS6()+"\t"+re.getS7()+"\t"+re.getS8()+"\t"+re.getS9());
        }
    }


    public static List<Tongji> ttt(String s ,String date){
        JSONObject jsonObject = JSONObject.parseObject(s);
        List<Tongji> list = new ArrayList<>();
        JSONArray buckets = jsonObject.getJSONObject("aggregations").getJSONObject("appCnName").getJSONArray("buckets");
        for (Object bucket : buckets) {
            JSONObject bt = (JSONObject) bucket;
            String appName = bt.getString("key");
            JSONArray bt2 = bt.getJSONObject("backServiceIp").getJSONArray("buckets");
            for (Object o : bt2) {
                JSONObject bt3 = (JSONObject) o;
                String backServiceIp = bt3.getString("key");
                JSONArray bt4 = bt3.getJSONObject("apiPath").getJSONArray("buckets");

                for (Object o1 : bt4) {
                    JSONObject bt5 = (JSONObject) o1;
                    String apiPath = bt5.getString("key");
                    String doc_count = bt5.getString("doc_count");
                    list.add(new Tongji(appName,backServiceIp+apiPath,doc_count,date));
                }
            }
        }
        return list;
    }




}
